package sg.edu.nus.iss.billing.ui.gui;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JTabbedPane;

import sg.edu.nus.iss.billing.service.login.Administrator;
import sg.edu.nus.iss.billing.ui.controller.OneBillingSystem;
import sg.edu.nus.iss.billing.vo.account.AccountVO;
import sg.edu.nus.iss.billing.vo.subscription.*;;

public class TabbedFrame extends JInternalFrame {
	
	private static final long serialVersionUID = 1L;
	private JTabbedPane tab;
	private OneBillingSystem onebillingsystem;
	private List<SubscriptionVO> availableplanoptions;
	private List<SubscriptionVO> subscripedplanoptions;
	private Hashtable<Integer, SubscriptionVO> htsubscripedplanoptions;
	private AccountVO accountvo;
	private String acctNo;
	
	public TabbedFrame(String name, OneBillingSystem onebillingsystem, String acctNo){
		super(name);
		this.setOnebillingsystem(onebillingsystem);
		tab = new JTabbedPane();
		this.acctNo=acctNo;
		setInitialValues();
		add(tab);
		//this.set
	}

	private void setInitialValues() {
		setAvailableplanoptions(onebillingsystem.getPlans());
		System.out.println("In TabbedFrame: acctNo : " + acctNo);
		htsubscripedplanoptions= new Hashtable<Integer, SubscriptionVO>();
		if(!acctNo.equalsIgnoreCase("" )){
			try{
				System.out.println("In TabbedFrame: acctNo Not null");
				accountvo = ((Administrator)onebillingsystem.getUser()).getAccountDetailsByAccountNumber(acctNo);
				Iterator<SubscriptionVO> iterator = accountvo.getListSubscriptionVO().iterator();
				SubscriptionVO plan;
				while (iterator.hasNext()) {
					plan= iterator.next();
					if( plan instanceof DigitalVoiceVO){
						htsubscripedplanoptions.put(0, plan);
					}
					else if( plan instanceof MobileVoiceVO){
						htsubscripedplanoptions.put(1, plan);
					}
					else if( plan instanceof CableTvVO){
						htsubscripedplanoptions.put(2, plan);
					}
				}
			}catch(Exception e){
				System.out.println(e.toString());
			}
			if (accountvo == null){
				accountvo= new AccountVO();
				subscripedplanoptions = new ArrayList<SubscriptionVO>();
			}
			else{
				subscripedplanoptions = accountvo.getListSubscriptionVO();
			}
				
			if(subscripedplanoptions== null)
				subscripedplanoptions = new ArrayList<SubscriptionVO>();
		}
		else{
			accountvo= new AccountVO();
			subscripedplanoptions = new ArrayList<SubscriptionVO>();
		}

		
	}

	public JTabbedPane getTab() {
		return tab;
	}

	public void setTab(JTabbedPane tab) {
		this.tab = tab;
	}

	public List<SubscriptionVO> getAvailableplanoptions() {
		return availableplanoptions;
	}

	public void setAvailableplanoptions(List<SubscriptionVO> availableplanoptions) {
		this.availableplanoptions = availableplanoptions;
	}

	public List<SubscriptionVO> getSubscripedplanoptions() {
		return subscripedplanoptions;
	}

	public void setSubscripedplanoptions(int index, SubscriptionVO subscriptionVO) {
		System.out.println("TABBEDFRAME setSubscripedplanoptions : "+subscripedplanoptions.size());
		System.out.println("TABBEDFRAME VO Class  : "+subscriptionVO.getName());
		System.out.println("TABBEDFRAME Index : "+index);
		htsubscripedplanoptions.remove(index);
		htsubscripedplanoptions.put(index, subscriptionVO);
		
		System.out.println("TABBEDFRAME VO Class  : htsubscripedplanoptions() "+htsubscripedplanoptions.size());
	}

	public OneBillingSystem getOnebillingsystem() {
		return onebillingsystem;
	}

	public void setOnebillingsystem(OneBillingSystem onebillingsystem) {
		this.onebillingsystem = onebillingsystem;
	}

	public String getAcctNo() {
		return acctNo;
	}

	public void setAcctNo(String acctNo) {
		this.acctNo = acctNo;
	}
	
	public AccountVO getAccount() {
		return accountvo;
	}

	public void setAccount(AccountVO accountvo) {
		this.accountvo = accountvo;
	}

	public Hashtable<Integer, SubscriptionVO> getHtsubscripedplanoptions() {
		return htsubscripedplanoptions;
	}

	public void setHtsubscripedplanoptions(Hashtable<Integer, SubscriptionVO> htsubscripedplanoptions) {
		this.htsubscripedplanoptions = htsubscripedplanoptions;
	}

}
